package com.itbour.service.yjx.tmpl.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.dao.yjx.tmpl.YjxTmplSetDao;
import com.itbour.dto.com.TmplSetMediaDto;
import com.itbour.dto.tg.DesignInfoDto;
import com.itbour.dto.yjx.tmpl.YjxTmplSetInfoDto;
import com.itbour.dto.yjx.tmpl.YjxTmplSetRInfoDto;
import com.itbour.service.yjx.tmpl.YjxTmplGrpService;

/* * 	一键秀 模板组实现
 */
public class YjxTmplGrpServiceImpl implements YjxTmplGrpService {
	
	private YjxTmplSetDao yjxTmplSetDao;
	
	public Page pageTmplSetInfo(Map<String, Object> param, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxTmplSetDao.getYjxTmplSetCnt(param);//获取个数
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<YjxTmplSetInfoDto> data = yjxTmplSetDao.getYjxTmplSetInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	
	//添加模板组
	public void addTmplSet(YjxTmplSetInfoDto topicInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("title", topicInfo.getTitle());
		param.put("desc", topicInfo.getDesc());
		param.put("use",topicInfo.getUse());
		param.put("update_by", topicInfo.getUpdate_by());
		param.put("thumb", topicInfo.getThumb());
		yjxTmplSetDao.insertTmplSetInfo(param);
	}
	
	//修改模板组
	public void upTmplSet(YjxTmplSetInfoDto tmplSetDto) {
		Map<String, Object> param =new HashMap<String, Object>();
		if (tmplSetDto.getSet_id()!=null){
			param.put("set_id",tmplSetDto.getSet_id());
			if(tmplSetDto.getTitle() != null) {
				param.put("title", tmplSetDto.getTitle());
			}
			if(tmplSetDto.getDesc() != null) {
				param.put("desc", tmplSetDto.getDesc());
			}
			if(tmplSetDto.getOrder()!= null) {
				param.put("order", tmplSetDto.getOrder());
			}
			if(tmplSetDto.getUse()!= null) {
				param.put("use", tmplSetDto.getUse());
			}
			if(tmplSetDto.getPublish() != null) {
				param.put("publish", tmplSetDto.getPublish());
			}
			if(tmplSetDto.getThumb() != null) {
				param.put("thumb", tmplSetDto.getThumb());
			}
			yjxTmplSetDao.updTmplSetInfoById(param);
		}
	}
	
	public void upTmplSet(Map<String, Object> parm){
		yjxTmplSetDao.updTmplSetInfoById(parm);
	}

	// 获取 单个模板组信息
	public List<YjxTmplSetInfoDto> getOneTmplInfo(Map<String, Object> parm) {
		return yjxTmplSetDao.getYjxTmplSetInfo(parm);
	}
	
	
	/*   * 获取模板组下的  设计 模板列表
	 */
	public Page getTmplGroupDesignInfo(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxTmplSetDao.getTmplGroupDesignCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<YjxTmplSetRInfoDto> data = yjxTmplSetDao.getTmplGroupDesignInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}
	
	//	获取适合的PPT或单页设计模板
	public Page getSuitDesignInfo(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxTmplSetDao.selectSuitDesignCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<DesignInfoDto> data = yjxTmplSetDao.getSuitDesign(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}		
	
	//	 获取适合的视频设计模板
	public Page getSuitViDesignInfo(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxTmplSetDao.selectSuitViDesignCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<YjxTmplSetRInfoDto> data = yjxTmplSetDao.getSuitViDesign(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}		
	
	// 添加 适合的模板 到模板组下
	public boolean addSuitDesign(Map<String, Object> parm){
		try{
			int in = yjxTmplSetDao.getTmplPriceId(parm);
			if (in>0){
				yjxTmplSetDao.addSuitDesign(parm);
			} else {
				parm.put("price_id", 1);
				yjxTmplSetDao.addPriceRInfo(parm);
				yjxTmplSetDao.addSuitDesign(parm);// 添加完了 再添加到模板组
			}
			return true;
		}catch(Exception e){
			return false;
		}
	}
	// 添加 适合的媒体文件 到模板组下
	public boolean addSuitMedia(Map<String, Object> parm){
		try{
			yjxTmplSetDao.addSuitDesign(parm);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean delTmplSRDesign(Map<String, Object> parm){
		try{
			if (parm.get("ids") !=null){
				yjxTmplSetDao.delTmplSRInfo(parm);
			}
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean delTmplGp(Map<String, Object> parm) {
		try {
			yjxTmplSetDao.delTmplSInfo(parm);
			yjxTmplSetDao.delTmplSRInfo(parm);// 删除模板组 关联信息
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	
	// 获取适合媒体文件列表
	public Page getSuitMediaInfo(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxTmplSetDao.selectSuitMediaCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<TmplSetMediaDto> data = yjxTmplSetDao.getSuitMedia(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}
	
	// **********************以下是生成的set和get方法 统一管理
	public YjxTmplSetDao getYjxTmplSetDao() {
		return yjxTmplSetDao;
	}
	public void setYjxTmplSetDao(YjxTmplSetDao yjxTmplSetDao) {
		this.yjxTmplSetDao = yjxTmplSetDao;
	}

	
	// 修改媒体的排序
	public void updMediaOrder(Map<String, Object> parm) {
		yjxTmplSetDao.updMediaOrder(parm);
	}
}
